package com.phone580.app.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.phone580.app.BaseActivity;
import com.phone580.app.CommonData;
import com.phone580.app.MainApplication;
import com.phone580.app.R;
import com.phone580.app.about.AboutActivity;
import com.phone580.app.untilty.CommonUtility;

public class SettingActivity extends BaseActivity {
	private ToggleButton tgbtnNotify, tgbtnWiFi;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		initHeader("设置");
		initToggleButton();
		RelativeLayout rlAbout = (RelativeLayout) findViewById(R.id.setting_about_rl);
		rlAbout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingActivity.this, AboutActivity.class);
				startActivity(intent);
			}
		});
		RelativeLayout rlUpdate = (RelativeLayout) findViewById(R.id.setting_update_rl);
		rlUpdate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				UpdateTask task = new UpdateTask(SettingActivity.this);
				task.execute();
			}
		});
		
		RelativeLayout rlWiFi = (RelativeLayout) findViewById(R.id.seting_only_wifi_rl);
		rlWiFi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tgbtnWiFi.toggle();
				if (tgbtnWiFi.isChecked()) {
					CommonData.ONLY_WIFI = true;
				} else {
					CommonData.ONLY_WIFI = false;
				}
				CommonUtility.storeValue(SettingActivity.this, "only_wifi", String.valueOf(CommonData.ONLY_WIFI));
//				((MainApplication)SettingActivity.this.getApplication()).callback.networkSettingChange();
			}
		});
		tgbtnWiFi.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (tgbtnWiFi.isChecked()) {
					CommonData.ONLY_WIFI = true;
				} else {
					CommonData.ONLY_WIFI = false;
				}
				CommonUtility.storeValue(SettingActivity.this, "only_wifi", String.valueOf(CommonData.ONLY_WIFI));
//				((MainApplication)SettingActivity.this.getApplication()).callback.networkSettingChange();
			}
		});
		
		RelativeLayout rlNotify = (RelativeLayout) findViewById(R.id.setting_notification_open_rl);
		rlNotify.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tgbtnNotify.toggle();
				if (tgbtnNotify.isChecked()) {
					CommonData.Notification = true;
				} else {
					CommonData.Notification = false;
				}
				CommonUtility.storeValue(SettingActivity.this, "notification", String.valueOf(CommonData.Notification));
			}
		});
		tgbtnNotify.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (tgbtnNotify.isChecked()) {
					CommonData.Notification = true;
				} else {
					CommonData.Notification = false;
				}
				CommonUtility.storeValue(SettingActivity.this, "notification", String.valueOf(CommonData.Notification));
			}
		});
	}
	
	private void initToggleButton() {
		tgbtnNotify = (ToggleButton) findViewById(R.id.setting_notification_open_tgbtn);
//		tgbtnNotify.setChecked(true);
		tgbtnWiFi = (ToggleButton) findViewById(R.id.seting_only_wifi_tgbtn);
		if (CommonData.ONLY_WIFI) {
			tgbtnWiFi.setChecked(true);
		} else {
			tgbtnWiFi.setChecked(false);
		}
		if (CommonData.Notification) {
			tgbtnNotify.setChecked(true);
		} else {
			tgbtnNotify.setChecked(false);
		}
	}
}
